package br.com.condominio.controller;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;

import br.com.condominio.entity.Apartamento;
import br.com.condominio.entity.Responsavel;
import br.com.condominio.service.ResponsavelService;

@ViewScoped
@ManagedBean(name = "responsavelBean")
public class ResponsavelBean extends BaseBean {

	private static final long serialVersionUID = 1L;
	@ManagedProperty("#{responsavelService}")
	private ResponsavelService model;
	private Responsavel responsavel;
	private List<Responsavel> responsavels;
	private String nomeResponsavel;
	private boolean editMode;
	
	private Apartamento apartamentoSelecionado;

	public ResponsavelService getModel() {
		return model;
	}

	public void setModel(ResponsavelService model) {
		this.model = model;
	}

	public Responsavel getResponsavel() {
		if(responsavel == null){
			responsavel = new Responsavel();
		}
		return responsavel;
	}

	public void setResponsavel(Responsavel responsavel) {
		this.responsavel = responsavel;
	}

	public void salvar() {
		System.out.println("idf responsavel " + responsavel.getRespCodigo());
		
		if (responsavel.getRespCodigo() == null
				|| responsavel.getRespCodigo().intValue() == 0) {
			responsavel = model.persistResponsavel(responsavel);
			responsavel = new Responsavel();
			addInfoMessage("Responsavel criado com sucesso.");
		} else {
			model.updateResponsavel(responsavel);
			addInfoMessage("Responsavel alterado com sucesso.");
		}
	}

	public void delete() {
		model.deleteResponsavel(responsavel);
		if (nomeResponsavel != null && !nomeResponsavel.isEmpty()){
			responsavels = model.getResponsavelsByName(nomeResponsavel);
		}else{
			responsavels = model.getAllResponsavels();
		}

		// return "";
	}

	public void create() {
		this.responsavel = new Responsavel();
		this.editMode = true;
	}

	public void update() {
		this.editMode = true;
	}

	public void cancel() {
		this.editMode = false;
	}

	public void filtrarResponsavel(AjaxBehaviorEvent event) {
		if (nomeResponsavel != null && !nomeResponsavel.isEmpty()) {
			responsavels = model.getResponsavelsByName(nomeResponsavel);
		} else {
			responsavels = model.getAllResponsavels();
		}
	}

	public List<Responsavel> getResponsavels() {
		if (responsavels == null) {
			responsavels = model.getAllResponsavels();
		}
		return responsavels;
	}

	public void setResponsavels(List<Responsavel> responsavels) {
		this.responsavels = responsavels;
	}

	public String getNomeResponsavel() {
		return nomeResponsavel;
	}

	public void setNomeResponsavel(String nomeResponsavel) {
		this.nomeResponsavel = nomeResponsavel;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}

	public Apartamento getApartamentoSelecionado() {
		return apartamentoSelecionado;
	}

	public void setApartamentoSelecionado(Apartamento apartamentoSelecionado) {
		this.apartamentoSelecionado = apartamentoSelecionado;
	}
	
}
